<template>
  <div>
    <div class="option">
      <el-button @click="info.isShow = true" type="primary" round plain
        >新增</el-button
      >
    </div>
    <!-- 表格 -->
    <el-table :data="roleList" style="width: 100%">
      <el-table-column prop="id" label="ID" min-width="80"> </el-table-column>
      <el-table-column prop="rolename" label="角色名称" min-width="180">
      </el-table-column>
      <el-table-column prop="status" label="状态" min-width="180">
        <template v-slot="props">
          <el-button
            type="success"
            size="mini"
            round
            plain
            v-if="props.row.status === 1"
            >正常</el-button
          >
          <el-button type="danger" size="mini" round plain v-else
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template v-slot="props">
          <el-button
            @click="edit(props.row)"
            type="primary"
            size="mini"
            round
            plain
            >编辑</el-button
          >
          <el-button @click="del(props.row.id)" type="danger" size="mini" round plain>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 调用子组件 -->
    <Dialog ref="dialog" :info="info" />
  </div>
</template>

<script>
// 导入子组件
import Dialog from "./Dialog";
// 导入vuex辅助函数
import { mapState, mapActions,mapMutations } from "vuex";
export default {
  data() {
    return {
      info: {
        // 控制子组件显示状态
        isShow: false,
      },
    };
  },
  components: { Dialog },
  computed: {
    ...mapState("role", ["roleList"]),
  },
  methods: {
    ...mapActions("role", ["getRoleListAction"]),
    ...mapMutations('role',['roleDel']),
    // 删除
    del(id){
      this.$myConfirm(()=>{
        this.$axios.post('/roledelete',{id}).then(res=>{
          if(res.code===200){
            // 成功提示
            this.$message.success(res.msg);
            // 更新角色数据列表: 移除被删除的数据
            this.roleDel(id);
          }else{
            this.$message.error(res.msg);
          }
        })
      })
    },
    // 编辑按钮点击事件处理函数
    edit(row) {
      // 1-显示对话框组件
      this.info.isShow = true;
      // 2-将待编辑的角色信息传递给对话框组件
      this.$refs.dialog.setFormData(row);
    },
  },
  created() {
    // 请求角色列表
    this.getRoleListAction();
  },
};
</script>

<style>
</style>